package com.kye.express.utils;

import android.text.format.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateTimeUtils {

    /**
     * 获取现在时间
     *
     * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
     */
    public static String getStringDate(long currentTimeMillis) {
        Date currentTime = new Date(currentTimeMillis);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取现在时间 指定格式
     *
     * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
     */
    public static String getStringDate(long currentTimeMillis, String format) {
        Date currentTime = new Date(currentTimeMillis);
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(currentTime);
        return dateString;
    }


    public static String getStringDateTime(long currentTimeMillis) {
        Date currentTime = new Date(currentTimeMillis);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
        String dateString = formatter.format(currentTime);
        String timeString = formatter2.format(currentTime);
        return dateString + "T" + timeString;
    }

    public static String getStringDateTime1(long currentTimeMillis) {
        Date currentTime = new Date(currentTimeMillis);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }


    /**
     * 获取现在时间 年月日
     *
     * @return返回字符串格式 yyyy-MM-dd
     */
    public static String getStringDateByMounthDay(long currentTimeMillis) {
        Date currentTime = new Date(currentTimeMillis);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getStringByDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }


    public static String getCurrentTime() {
        String str = DateFormat.format("yyyy-M-d kk:mm:ss", Calendar.getInstance().getTime()).toString();//获得系统时间
        return str;
    }

    public static String getCurrentTimeNoSecond() {
        String str = DateFormat.format("yyyy-MM-dd kk:mm", Calendar.getInstance().getTime()).toString();//获得系统时间
        return str;
    }

    public static String getCurrentDate() {
        String str = DateFormat.format("yyyy-MM-dd", Calendar.getInstance().getTime()).toString();//获得系统时间
        return str;
    }


    public static String getCurrentDayAndMinue(long time) {
        String str = DateFormat.format("MM-dd HH:mm", time).toString();//获得系统时间
        return str;
    }


    /**
     * 将时间格式为yyyy-MM-dd HH:mm:ss转换为long
     */
    public static long getOderTime(String time) {
        long millionSeconds;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            millionSeconds = sdf.parse(time).getTime();//毫秒
            return millionSeconds;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 将时间格式为yyyy-MM-dd HH:mm:ss转换为long
     */
    public static long getSimpleLongTime(String time) {
        long millionSeconds;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            millionSeconds = sdf.parse(time).getTime();//毫秒
            return millionSeconds;
        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * 将时间格式为yyyy-MM-dd转换为long
     */
    public static long getSimpleLongDDTime(String time) {
        long millionSeconds;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            millionSeconds = sdf.parse(time).getTime();//毫秒
            return millionSeconds;
        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * 将自定义格式时间，转换为long
     */
    public static long getSimpleLongTime(String time, String format) {
        long millionSeconds;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            millionSeconds = sdf.parse(time).getTime();//毫秒
            return millionSeconds;
        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * 将时间格式为yyyy-MM-dd Date
     */
    public static Date strToDate(String time) {
        Date millionSeconds = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            millionSeconds = sdf.parse(time);//毫秒
            return millionSeconds;
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 与当前时间对比  是否是同一天
     *
     * @param time
     * @return
     */
    public static boolean isTheSameDay(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        int currentMonth = Calendar.getInstance().get(Calendar.MONTH);
        if (year == currentYear && month == currentMonth && day == currentDay) {
            return true;
        }
        return false;
    }

    /**
     * 获得当前系统日期(只获取年，用于航班日期判断显示日期有用)
     *
     * @return
     */
    public static String getFightDateTime() {
        SimpleDateFormat myFmt2 = new SimpleDateFormat("yyyy");
        Date now = new Date();
        String date = myFmt2.format(now);
        return date;
    }

    /**
     * 判断是否是工作时间
     *
     * @return
     */
    public static boolean isWorkTime() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
//        String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
//        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
//        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        int mHour = c.get(Calendar.HOUR_OF_DAY);//获取当前的小时数
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay) || "7".equals(mWay)) {// 1 是星期日  7是星期六
            return false;
//        }else if("2".equals(mWay) || "3".equals(mWay) || "4".equals(mWay) || "5".equals(mWay) || "6".equals(mWay)){
        } else {
            if (mHour < 9 || mHour >= 18) {
                return false;
            } else {
                return true;
            }
        }
    }

    /**
     * 接收消息提醒时间设置   8点至22点不提醒
     *
     * @return
     */
    public static boolean isSetingShow() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        int mHour = c.get(Calendar.HOUR_OF_DAY);//获取当前的小时数
        if (mHour >= 8 && mHour < 22) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 是否在当前时间区间内
     *
     * @param startTime 开始时间，时
     * @param endTime   结束时间，时
     * @return
     */
    public static boolean isTimeSpace(int startTime, int endTime) {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        int mHour = c.get(Calendar.HOUR_OF_DAY);//获取当前的小时数
        if (mHour >= startTime && mHour < endTime) {
            return true;
        } else {
            return false;
        }
    }

    /***
     *  获取指定日后 后 dayAddNum 天的 日期
     *  @param dayAddNum 增加天数 格式为int;
     *  @return
     */
    public static String getDateAdd(String date, int dayAddNum) {
        String dateOk = "";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date newDate2 = new Date(simpleDateFormat.parse(date).getTime() + dayAddNum * 24 * 60 * 60 * 1000);
            dateOk = simpleDateFormat.format(newDate2);
        } catch (Exception e) {
        }
        return dateOk;
    }

    /**
     * 转换日期格式
     *
     * @param date
     * @return
     */
    public static String changeFormat(String date) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d = simpleDateFormat.parse(date);
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            date = simpleDateFormat.format(d);
        } catch (ParseException e) {
        }
        return date;
    }

    /**
     * 转换日期格式
     *
     * @param date
     * @return
     */
    public static String changeDateFormat(String date) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d = simpleDateFormat.parse(date);
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            date = simpleDateFormat.format(d);
        } catch (ParseException e) {
        }
        return date;
    }

    /**
     * 对前一个星期或后一个星期日期的获取
     *
     * @param date
     * @param type 0获取后一个星期 1 获取前一个星期
     * @return
     */
    public static Date getWeekDayByType(Date date, int type) {
        String mYear; // 当前年
        String mMonth; // 月
        String mDay;
        int current_day;
        int current_month;
        int current_year;

        //设置当前时间
        final Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        current_year = c.get(Calendar.YEAR);
        current_month = c.get(Calendar.MONTH);
        current_day = c.get(Calendar.DAY_OF_MONTH);

        c.set(Calendar.MONTH, current_month);
        c.set(Calendar.DAY_OF_MONTH, current_day);
        c.set(Calendar.YEAR, current_year);
        if (type == 0) {
            c.add(Calendar.DATE, +7);//j记住是DATE
        } else {
            c.add(Calendar.DATE, -7);//j记住是DATE
        }
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前日份的日期号码
        mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
        String dateTime = mYear + "年" + mMonth + "月" + mDay + "日";
        return c.getTime();
    }

    /**
     * 判断是否超出指定时间 单位天数
     *
     * @param startMillon
     * @param endMillon
     * @param day
     * @return
     */
    public static boolean isExceed(long startMillon, long endMillon, int day) {
        int dayMillon = 24 * 60 * 60 * 1000;
        if ((endMillon - startMillon) > dayMillon * day) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否超出指定时间 单位天数
     *
     * @param startMillon
     * @param endMillon
     * @param day
     * @return
     */


    /**
     * 距离当前时间七天之内的日期，和七天之外的日期
     *
     * @param type type:1--7天之内的
     *             type:2--7天之外的
     * @return
     */
    public static boolean getDayDiffFromToday(Date startDay, Date endDay, int type) {
        if (endDay.getTime()< startDay.getTime()){
            return false;
        }
        long diff = endDay.getTime() - startDay.getTime();
        if (diff < 0) diff = 0;
        long days = diff / (1000 * 60 * 60 * 24);

        if (type == 1 && days >=0 && days < 7) return true;
        if (type == 2 && days > 7) return true;

        return false;
    }


    /**
     * 判断时间选择时间是否小于当前时间
     *
     * @param startMillon
     * @param endMillon
     * @param day
     * @return
     */
    public static boolean isBefore(long startMillon, long endMillon, int day) {
        int dayMillon = 24 * 60 * 60 * 1000;
        if (Math.abs(endMillon - startMillon) > dayMillon * day) {
            return true;
        } else {
            return false;
        }
    }
}